import 'package:flutter/material.dart';
import 'package:interview/components/app/app_animated_overlay.dart';
import 'package:interview/config/app.enum.dart';
import 'package:interview/theme/app_colors.dart';

class AppPromptOverlay {
  static OverlayEntry? _overlayEntry;

  static void show({
    required BuildContext context,
    required String message,
    bool showIcon = true,
    String position = 'center',
    int durationMilliseconds = 1500,
  }) {
    if (_overlayEntry != null) return;

    final overlayState = Overlay.of(context);
    final theme = Theme.of(context).extension<AppColors>()!;

    final promptWidget = Material(
      color: Colors.transparent,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12.0),
          color: theme.contrastPure,
          boxShadow: [
            BoxShadow(
              color: theme.contrastPure!.withOpacity(0.2), // 阴影颜色和透明度
              offset: Offset(0, 4), // 向下偏移4个像素
              blurRadius: 8.0, // 模糊半径为8个像素
              spreadRadius: 0, // 不改变阴影大小
            ),
          ],
        ),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            if (showIcon)
              ClipRRect(
                borderRadius: BorderRadius.circular(24.0),
                child: Image.asset("${AppEnumAssets.basePath}icon.png",
                    width: 24, height: 24),
              ),
            if (showIcon) SizedBox(width: 8),
            Text(
              message,
              style: TextStyle(
                  fontSize: 12,
                  fontWeight: FontWeight.w500,
                  color: theme.subjectPure),
            ),
          ],
        ),
      ),
    );

    // 动画起始和结束偏移量，根据位置动态设置
    Offset beginOffset;
    Offset endOffset = Offset.zero;

    switch (position.toLowerCase()) {
      case 'top':
        beginOffset = Offset(0, -1.0); // 从顶部进场
        break;
      case 'bottom':
        beginOffset = Offset(0, 1.0); // 从底部进场
        break;
      default: // 默认居中，
        beginOffset = Offset(1, 0);
    }


    // 使用 AppAnimatedOverlay 包裹
    _overlayEntry = OverlayEntry(
      builder: (BuildContext context) {
        return AppAnimatedOverlay(
          content: promptWidget,
          beginOffset: beginOffset,
          endOffset: endOffset,
          position: position,
          showOverlay: false,
          duration: const Duration(milliseconds: 200), // 动画时间
          onClose: hide, // 动画结束后关闭
          dismissOnBackgroundTap: true, // 点击蒙层不关闭
        );
      },
    );

    // 显示 OverlayEntry
    overlayState.insert(_overlayEntry!);

    // 自动关闭
    Future.delayed(Duration(milliseconds: durationMilliseconds), () {
      AppAnimatedOverlay.close();
    });
  }

  /// 隐藏提示框  将hide方法 绑定给AppAnimatedOverlay的onClose回调   onClose 会在动画执行完成后执行
  static void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }
}
